package com.sg.web;

import com.github.pagehelper.PageInfo;
import com.sg.bean.*;
import com.sg.service.InvitationService;
import com.sg.service.InvitationStateService;
import com.sg.service.InvitationTypeService;
import com.sg.service.RemarkService;
import com.sg.vo.InvitationVO;
import com.sg.vo.RemarkVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author fyp
 * 帖子控制器
 */
@Controller
public class InvitationController {

    Logger logger=Logger.getLogger(InvitationController.class);

    @Autowired
    InvitationService invitationService;

    @Autowired
    InvitationStateService invitationStateService;

    @Autowired
    InvitationTypeService invitationTypeService;

    @Autowired
    RemarkService remarkService;

    //分页查询帖子列表
    @RequestMapping("/invitationlist")
    public String invitationlist(String pageNum, @RequestParam(defaultValue = "5") int pageSize,Model model){
        //获得当前页码和查询条件
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        //判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        //查询到一个集合，pageNum页数pageSize
        List<InvitationVO> volist=invitationService.findAllPage(Integer.parseInt(pageNum),pageSize);
        //把集合封装到一个对象page中

        PageInfo pageInfo=new PageInfo(volist);
        Page page=new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/invitationlist.action");
        //把page对象存储到model中，在当前页面取出
        model.addAttribute("page",page);
        //返回页面
        return "/invitationlist";
    }

    //进入添加帖子控制器
    @RequestMapping("/user/toinvitationadd")
    public String toInvitationadd(Model model, HttpSession session){
        logger.info("进入添加帖子控制器");
        List<InvitationType> list =invitationTypeService.findAll();
        session.getAttribute("createUser");
        model.addAttribute("list",list);
        return "/user/invitationadd";
    }

    //添加帖子
    @RequestMapping("/user/invitationadd")
    public String invitationadd(Invitation invitation){
        logger.info("添加帖子");
        boolean flag=invitationService.add(invitation);
        return "redirect:/invitationlist.action";
    }

    //删除帖子
    @RequestMapping("/invitationdelete")
    public String invitationdelete(int invitationId){
        logger.info("删除帖子");
        boolean flag=invitationService.delete(invitationId);
        logger.info("flag");
        return "redirect:/invitationlist.action";
    }

    //进入帖子状态表
    @RequestMapping("/admin/invitationstatelist")
    public String invitationStatelist(Model model){
        logger.info("进入帖子状态表");
        List<InvitationState> list=invitationStateService.findAll();
        model.addAttribute("list",list);
        return "/admin/invitationstatelist";
    }

    //进入帖子类型表
    @RequestMapping("/admin/invitationtypelist")
    public String invitationtypelist(String pageNum, @RequestParam(defaultValue = "5") int pageSize,Model model){
        //获得当前页码和查询条件
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        //判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        //查询到一个集合，pageNum页数pageSize
        List<InvitationType> volist=invitationTypeService.findAllPage(Integer.parseInt(pageNum),pageSize);
        //把集合封装到一个对象page中
        PageInfo pageInfo=new PageInfo(volist);
        Page page=new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/admin/invitationtypelist.action");
        //把page对象存储到model中，在当前页面取出
        model.addAttribute("page",page);
        //返回页面
        return "/admin/invitationtypelist";
    }

    //进入添加类型控制器
    @RequestMapping("/admin/toinvitationtypeadd")
    public String toinvitationtypeadd(){
        logger.info("进入添加类型控制器");
        return "/admin/invitationtypeadd";
    }

    //添加类型
    @RequestMapping("/admin/invitationtypeadd")
    public String invitationtypeadd(InvitationType invitationType){
        logger.info("添加类型");
        boolean flag=invitationTypeService.add(invitationType);
        return "redirect:/admin/invitationtypelist.action";
    }

    //删除类型
    @RequestMapping("/admin/invitationtypedelete")
    public String invitationtypedelete(int itId){
        logger.info("删除类型");
        boolean flag=invitationTypeService.delete(itId);
        logger.info("flag");
        return "redirect:/admin/invitationtypelist.action";
    }

    //进入修改控制器
    @RequestMapping("/admin/toinvitationupdate")
    public String toinvitationupdate(Model model,Integer invitationId){
        logger.info("进入修改控制器");
        List<InvitationVO> list=invitationService.findAll();
        InvitationVO invitationVO=invitationService.findById(invitationId);
        List<InvitationState> list1=invitationStateService.findAll();
        List<InvitationType> list2=invitationTypeService.findAll();
        model.addAttribute("invitationVO",invitationVO);
        model.addAttribute("list",list);
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        return "/admin/invitationupdate";
    }

    //进入修改
    @RequestMapping("/admin/invitationupdate")
    public String invitationupdate(InvitationVO invitationVO){
        logger.info("进入修改");
        boolean flag=invitationService.update(invitationVO);
        return "redirect:/admin/invitationlist.action";
    }
    //进入审核通过
    @RequestMapping("/admin/check")
    public String check(int invitationId){
        logger.info("进入审核通过");
        InvitationVO invitationVO=invitationService.findById(invitationId);
        invitationVO.setIsId(2);
        boolean flag=invitationService.update(invitationVO);
        return "redirect:/admin/invitationchecktwo.action";
    }

    //进入审核不通过
    @RequestMapping("/admin/checktwo")
    public String checktwo(int invitationId){
        logger.info("进入审核不通过");
        InvitationVO invitationVO=invitationService.findById(invitationId);
        invitationVO.setIsId(3);
        boolean flag=invitationService.update(invitationVO);
        return "redirect:/admin/invitationchecktwo.action";
    }

    //进入审核通过界面
    @RequestMapping("/admin/invitationcheck")
    public String invitationcheck(Model model){
        logger.info("进入审核页面");
        List<InvitationVO> list=invitationService.findByName("审核通过");
        logger.info("list.size"+list.size());
        logger.info(list);
        model.addAttribute("invitationlist",list);
        logger.info(list);
        return "/admin/invitationcheck";
    }

    //进入待审核界面
    @RequestMapping("/admin/invitationchecktwo")
    public String invitationchecktwo(Model model){
        logger.info("进入待审核页面");
        List<InvitationVO> list=invitationService.findByName("待审核");
        logger.info("list.size"+list.size());
        logger.info(list);
        model.addAttribute("invitationlist",list);
        logger.info(list);
        return "/admin/invitationchecktwo";
    }

    //进入审核不通过界面
    @RequestMapping("/admin/invitationcheckthree")
    public String invitationcheckthree(Model model){
        logger.info("进入审核不通过页面");
        List<InvitationVO> list=invitationService.findByName("审核不通过");
        logger.info("list.size"+list.size());
        logger.info(list);
        model.addAttribute("invitationlist",list);
        logger.info(list);
        return "/admin/invitationcheckthree";
    }

    //进入评论列表
    @RequestMapping("/remarklist")
    public String remarklist(String pageNum, @RequestParam(defaultValue = "8") int pageSize,Model model){
        //获得当前页码和查询条件
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        //判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        //查询到一个集合，pageNum页数pageSize
        List<RemarkVO> volist=remarkService.findAllPage(Integer.parseInt(pageNum),pageSize);
        //把集合封装到一个对象page中
        PageInfo pageInfo=new PageInfo(volist);
        Page page=new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/remarklist.action");
        //把page对象存储到model中，在当前页面取出
        model.addAttribute("page",page);
        //返回页面
        return "/remarklist";
    }

    //进入添加评论
    @RequestMapping("/user/toremarkadd")
    public String toremarkadd(Model model){
        logger.info("进入添加评论控制器");
        List<InvitationVO> list=invitationService.findAll();
        model.addAttribute("list",list);
        return "/user/remarkadd";
    }

    //添加评论
    @RequestMapping("/user/remarkadd")
    public String remarkadd(Remark remark){
        logger.info("添加评论");
        boolean flag=remarkService.add(remark);
        logger.info("flag");
        return "redirect:/remarklist.action";
    }

    //删除评论
    @RequestMapping("/remarkdelete")
    public String remarkdelete(int[] remarkId){
        logger.info("删除评论");
        boolean flag=remarkService.delete(remarkId);
        logger.info("flag");
        return "redirect:/remarklist.action";
    }

    //分页查询帖子列表
    @RequestMapping("/invitation")
    public String invitation(String pageNum, @RequestParam(defaultValue = "5") int pageSize,Model model){
        //获得当前页码和查询条件
        if(pageNum==null||"".equals(pageNum)){
            pageNum="1";
        }
        //判断首次进入时，点击用户管理，默认当前页码是1，查询条件是null
        //查询到一个集合，pageNum页数pageSize
        List<InvitationVO> volist=invitationService.findAllPage(Integer.parseInt(pageNum),pageSize);
        //把集合封装到一个对象page中

        PageInfo pageInfo=new PageInfo(volist);
        Page page=new Page();
        page.setPageInfo(pageInfo);
        //动态地址和动态查询条件的封装，用于回显
        page.setServletUrl("/invitation.action");
        //把page对象存储到model中，在当前页面取出
        model.addAttribute("page",page);
        //返回页面
        return "/index";
    }
}
